﻿using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp32
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private bool isDragging = false;
        private DrawingVisual? selectedVisual;
        private Vector clickOffset;

        private void DrawSquare(DrawingVisual visual, Point point, bool selected)
        {
            using DrawingContext dc = visual.RenderOpen();

            Brush brush = Brushes.Black;
            if (selected)
            {
                brush = Brushes.LightBlue;
            }

            Pen pen = new(brush, 2);
            Point p = new Point(point.X + 40, point.Y + 40);

            dc.DrawRectangle(Brushes.White, pen, new Rect(point, p));
        }

        private void drawingSurface_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Point pointClick = e.GetPosition(drawingSurface);

            if (cmdAdd.IsChecked == true)
            {
                //添加一个Visual
                DrawingVisual visual = new();
                DrawSquare(visual, pointClick, false);

                drawingSurface.AddVisual(visual);
            }
            else if (cmdMove.IsChecked == true)
            {
                //命中测试，得到这个Visual对象
                DrawingVisual? visual = drawingSurface.GetDrawingVisual(pointClick);
                if (visual != null)
                {
                    //要重新计算举行的原点位置
                    Point point = new(visual.ContentBounds.TopLeft.X + 1, visual.ContentBounds.TopLeft.Y + 1);

                    DrawSquare(visual, point, true);

                    clickOffset = point - pointClick; //计算向量偏值，

                    selectedVisual = visual;

                    isDragging = true;
                }
            }
            else if (cmdDelete.IsChecked == true)
            {
                DrawingVisual? visual = drawingSurface.GetDrawingVisual(pointClick);
                if (visual != null)
                {
                    drawingSurface.DeleteVisual(visual);
                }
            }
        }
        private void drawingSurface_MouseMove(object sender, MouseEventArgs e)
        {
            if (isDragging && selectedVisual != null)
            {
                Point point = e.GetPosition(drawingSurface) + clickOffset;
                DrawSquare(selectedVisual, point, true);
            }
        }

        private void drawingSurface_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            isDragging = false;

            //还原
            if (selectedVisual != null)
            {
                Point point = new(selectedVisual.ContentBounds.TopLeft.X + 1, selectedVisual.ContentBounds.TopLeft.Y + 1);
                DrawSquare(selectedVisual, point, false);
            }
        }

    
    }
}